package com.siao.component
{
	import com.siao.display.LayoutElement;
	import com.siao.display.Range;
	
	/**
	 * 画布
	 * @author SiaoLeon
	 */
	public class Canvas extends LayoutElement
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		private var _width:Number;
		
		override public function get width():Number
		{
			return this._width;
		}
		
		override public function set width(value:Number):void
		{
			this._width = value;
			
			this.layout();
		}
		
		private var _height:Number;
		
		override public function get height():Number
		{
			return this._height;
		}
		
		override public function set height(value:Number):void
		{
			this._height = value;
			
			this.layout();
		}
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function Canvas(width:Number, height:Number)
		{
			super();
			
			this._width = width;
			this._height = height;
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		override public function resize():void
		{
			if (this.stage == null || !this.visible)
			{
				return;
			}
			
			var rect:Range = this.getAvailableRange();
			var rectWidth:Number = rect.width - rect.left - rect.right - this.marginLeft - this.marginRight;
			var rectHeight:Number = rect.height - rect.top - rect.bottom - this.marginTop - this.marginBottom;
			
			var newWidht:Number = rectWidth;
			var newHeight:Number = this.height * rectWidth / this.width
			
			if (newHeight > rectHeight)
			{
				newWidht = this.width * rectHeight / this.height;
				newHeight = rectHeight;
			}
			
			this._width = newWidht;
			this._height = newHeight;
			
			//			this.scaleX = newWidht / this.width;
			//			this.scaleY = newHeight / this.height;
			
			super.resize();
		}
	
		// -------------------------------------------------------------------------------------------------------- 函数
	
	}
}
